<?php
class ArticlesController extends AppController {
	public $name = 'Articles';
	public $uses = array('Article','Comment','Period');
	public $components = array('Webservice.Webservice','RequestHandler');
	public $viewClass = 'Webservice.Webservice';
	
	function getAllArticles(){
		$arrArticles = $this->Article->getListArticles();
		if(isset($arrArticles)){
			$dataOutput->massage = "successfully";
			$dataOutput->success = true;
			$i = 0;
			foreach($arrArticles as $article){
				$articles[$i]['Article']['id'] = $article['Article']['id'];
				$articles[$i]['Article']['hero_id'] = $article['Article']['hero_id'];
				$articles[$i]['Article']['title'] = $article['Article']['title'];
				$articles[$i]['Article']['number_of_read'] = $article['Article']['number_of_read'];
				$articles[$i]['Periods'] = $this->Period->getPeriodsByHeroId($article['Article']['hero_id']);
				$i++;
			}
			$dataOutput->response = $articles;
		}else{
			$dataOutput->massage = 'no database';
			$dataOutput->success = false;
		}
		$this->set(compact('dataOutput'));
	}
	
	function getArticlesByHeroId($id = null){
		if(isset($id)){
			$arrArticles = $this->Article->getListArticlesByHeroId($id);
			if(isset($arrArticles)){
				$dataOutput->massage = "successfully";
				$dataOutput->success = true;
				$i = 0;
				foreach($arrArticles as $article){
					$articles[$i]['Article']['id'] = $article['Article']['id'];
					$articles[$i]['Article']['hero_id'] = $article['Article']['hero_id'];
					$articles[$i]['Article']['title'] = $article['Article']['title'];
					$articles[$i]['Article']['number_of_read'] = $article['Article']['number_of_read'];
					$articles[$i]['Periods'] = $this->Period->getPeriodsByHeroId($article['Article']['hero_id']);
					$i++;
				}
				$dataOutput->response = $articles;
			}else{
				$dataOutput->massage = 'no database';
				$dataOutput->success = false;
			}
		}else{
			$dataOutput->massage = 'please! input hero id';
			$dataOutput->success = false;
		}
		$this->set(compact('dataOutput'));
	}
	
	function getArticleById($id = null){
		if(isset($id)){
			$articles = $this->Article->getArticleById($id);
			if(!empty($articles)){
				$dataOutput->massage = "successfully";
				$dataOutput->success = true;
				$countComments = $this->Comment->countCommentsByArticleId($id);
				if(isset($countComments)){
					$dataOutput->numberOfComments = $countComments;
				}else{
					$dataOutput->numberOfComments = 0;
				}
				//tang so lan doc bai viet
				$this->Article->addNumberOfRead($id);
				$dataOutput->response = $articles;
			}else{
				$dataOutput->massage = 'no database';
				$dataOutput->success = false;
			}
		}else{
			$dataOutput->massage = 'please! input hero id';
			$dataOutput->success = false;
		}
		$this->set(compact('dataOutput'));
	}
	function getArticlesByName($name = null){
		if(isset($name)){
			$arrArticles = $this->Article->getArticlesByName($name);
		}else{
			$arrArticles = $this->Article->getListArticles();
		}
		if(!empty($arrArticles)){
			$dataOutput->massage = "successfully";
			$dataOutput->success = true;
			$i = 0;
			foreach($arrArticles as $article){
				$articles[$i]['Article']['id'] = $article['Article']['hero_id'];
				$articles[$i]['Article']['hero_id'] = $article['Article']['hero_id'];
				$articles[$i]['Article']['title'] = $article['Article']['title'];
				$articles[$i]['Article']['number_of_read'] = $article['Article']['number_of_read'];
				$articles[$i]['Periods'] = $this->Period->getPeriodsByHeroId($article['Article']['hero_id']);
				$i++;
			}
			$dataOutput->response = $articles;
		}else{
			$dataOutput->massage = 'no database';
			$dataOutput->success = false;
		}
		$this->set(compact('dataOutput'));
	}
}
?>